package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

public class LC503NextGreaterElment {

    public int[] nextGreaterElements(int[] nums) {
        int[] ret = new int[nums.length];
        LinkedList<Integer> stack = new LinkedList<>();
        Arrays.fill(ret,-1);
        for (int i = 0; i < nums.length * 2 - 1; i++) {
            while (!stack.isEmpty() && nums[stack.peek()] < nums[i % nums.length]) {
                ret[stack.pop()] = nums[i % nums.length];
            }
            stack.push(i % nums.length);
        }
        return ret;
    }
}
